/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import controller.exceptions.NonexistentEntityException;
import entidades.Cargos;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

/**
 *
 * @author Renato
 */
public class CargosJpaController {

    private Integer id;
    private String cargo;
    private double salarioBase;
    public Cargos cargos;
    public List<Cargos> listCargos;
    public Integer idSelecionado;

    public String novoCargo() {
        this.cargos = new Cargos();
        return "novoCargo";
    }

    public String listarCargos(){
        return "listarCargos";
    }

    public String excluirCargo() throws NonexistentEntityException{
        cargos = findCargos(idSelecionado);
        destroy(idSelecionado);
        return "listarCargos";
    }
    
   public String editarCargo(){
       cargos = findCargos(idSelecionado);
       return "editarCargo";
   }

    public CargosJpaController() {
        emf = Persistence.createEntityManagerFactory("restaurantePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public String create() {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(cargos);
            em.getTransaction().commit();
            FacesMessage fm = new FacesMessage("Cargo '"+cargos.getCargo()+"' inserido");
            FacesContext.getCurrentInstance().addMessage("Cargo inserido", fm);
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return "listarCargos";
    }

    public String edit() throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            cargos = em.merge(cargos);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id1 = cargos.getId();
                if (findCargos(id1) == null) {
                    throw new NonexistentEntityException("The cargos with id " + id1 + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return "listarCargos";
    }

    public String destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cargos cargos1;
            try {
                cargos1 = em.getReference(Cargos.class, id);
                cargos1.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cargos with id " + id + " no longer exists.", enfe);
            }
            em.remove(cargos1);
            em.getTransaction().commit();
            FacesMessage fm = new FacesMessage("Cargo '"+cargos1.getCargo()+"' excluido");
            FacesContext.getCurrentInstance().addMessage("Cargo excluido", fm);
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return "listarCargos";
    }

    public List<Cargos> getfindCargosEntities() {
        return findCargosEntities(true, -1, -1);
    }

    public List<Cargos> findCargosEntities(int maxResults, int firstResult) {
        return findCargosEntities(false, maxResults, firstResult);
    }

    private List<Cargos> findCargosEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Cargos as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cargos findCargos(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cargos.class, id);
        } finally {
            em.close();
        }
    }

    public int getCargosCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Cargos as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public Integer getIdSelecionado() {
        return idSelecionado;
    }

    public void setIdSelecionado(Integer idSelecionado) {
        this.idSelecionado = idSelecionado;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCargo() {
        return cargo;
    }

    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    public double getSalarioBase() {
        return salarioBase;
    }

    public void setSalarioBase(double salarioBase) {
        this.salarioBase = salarioBase;
    }

    public Cargos getCargos() {
        return cargos;
    }

    public void setCargos(Cargos cargo) {
        this.cargos = cargo;
    }
    
    public List<Cargos> getlistCargos() {
        listCargos = getfindCargosEntities();
        return listCargos;
    }

    public void setlistCargos(List<Cargos> listCargos) {
        this.listCargos = listCargos;
    }

    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "---");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    public SelectItem[] getCargosItemsAvailableSelectOne() {
        return getSelectItems(getfindCargosEntities(), true);
    }

}
